/*
 * GloMoSim is COPYRIGHTED software.  Release 2.02 of GloMoSim is available 
 * at no cost to educational users only.
 *
 * Commercial use of this software requires a separate license.  No cost,
 * evaluation licenses are available for such purposes; please contact
 * info@scalable-networks.com
 *
 * By obtaining copies of this and any other files that comprise GloMoSim2.02,
 * you, the Licensee, agree to abide by the following conditions and
 * understandings with respect to the copyrighted software:
 *
 * 1.Permission to use, copy, and modify this software and its documentation
 *   for education and non-commercial research purposes only is hereby granted
 *   to Licensee, provided that the copyright notice, the original author's
 *   names and unit identification, and this permission notice appear on all
 *   such copies, and that no charge be made for such copies. Any entity
 *   desiring permission to use this software for any commercial or
 *   non-educational research purposes should contact: 
 *
 *   Professor Rajive Bagrodia 
 *   University of California, Los Angeles 
 *   Department of Computer Science 
 *   Box 951596 
 *   3532 Boelter Hall 
 *   Los Angeles, CA 90095-1596 
 *   rajive@cs.ucla.edu
 *
 * 2.NO REPRESENTATIONS ARE MADE ABOUT THE SUITABILITY OF THE SOFTWARE FOR ANY
 *   PURPOSE. IT IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
 *
 * 3.Neither the software developers, the Parallel Computing Lab, UCLA, or any
 *   affiliate of the UC system shall be liable for any damages suffered by
 *   Licensee from the use of this software.
 */

// Use the latest version of Parsec if this line causes a compiler error.
/*
 * $Id: tcplib.c,v 1.4 1999/09/05 05:21:20 jmartin Exp $
 *
 * Ported from TCPLIB.  This file contains tcplib() and lookup(). 
 */

/*
 * Copyright (c) 1991 University of Southern California.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms are permitted
 * provided that the above copyright notice and this paragraph are
 * duplicated in all such forms and that any documentation,
 * advertising materials, and other materials related to such
 * distribution and use acknowledge that the software was developed
 * by the University of Southern California. The name of the University 
 * may not be used to endorse or promote products derived from this 
 * software without specific prior written permission.
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include "tcplib.h"

extern long pc_nrand(unsigned short seed[3]);

int lookup (struct histmap *hmp, char *h_name)
{
  int i;
  struct histmap *p;

  for (i = 0, p = hmp; i < MAXHIST && p->h_name != 0 && strcmp(p->h_name, h_name); i++, ++p);
  if (i == MAXHIST) {
    perror("Too many characteristics.");
    exit(1);
  }
  if (p->h_name == 0) {
    perror("Characteristic not found.");
    exit(1);
  }

  return(i);
}

#define MAXRAND 2147483647
#define uniform_rand(from, to, seed)\
  ((from) + (((double) pc_nrand((seed))/MAXRAND) * ((to) - (from))))


double tcplib(struct histmap *hmp, struct entry *tbl, unsigned short seed[3])
{
  float prob;
  int maxbin = hmp->nbins-1;
  int base = 0;
  int bound = maxbin;
  int mid = (int)((float)maxbin / 2.0 + 0.5);
  
  prob = ((float) (pc_nrand(seed) % PRECIS)) / (float) PRECIS;

  do {
    while (prob <= tbl[mid-1].prob && mid != 1) {
      bound = mid;
      mid -= (int)(((float)(mid - base)) / 2.0 + 0.5);
    } 
    while (prob > tbl[mid].prob) {
      base = mid;
      mid += (int)(((float)(bound - mid)) / 2.0 + 0.5);
    }
  } while (!(mid == 1 || (prob >= tbl[mid-1].prob && prob <= tbl[mid].prob)));
      
  if (mid == 1 && prob < tbl[0].prob) {
    return ((double) tbl[0].value);
  } else {
    return ((double) uniform_rand(tbl[mid-1].value, tbl[mid].value, seed));
  }
}

